package com.aoxin.service.impl.client;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import com.aoxin.mapper.client.ClientIndexDao;
import com.aoxin.service.inter.client.ClientIndexService;
import com.aoxin.util.ErrorUtil;
import com.aoxin.util.ThreadLocalUtil;
import com.aoxin.util.enumerate.ArticleType;

@Service("ClientIndexService")
public class ClientIndexServiceImpl implements ClientIndexService {
    @Autowired
    private ClientIndexDao ClientIndexDao;

    @Override
    public LinkedList<Map<String, Object>> getUserInfo() throws Exception {
        String phone = ThreadLocalUtil.getUser().getPhone();
        Assert.notNull(phone, ErrorUtil.INDEX_USER_INFO);
        LinkedList<Map<String, Object>> userInfo = ClientIndexDao.getUserInfo(phone);
        if (userInfo != null && !userInfo.isEmpty()) {
            return userInfo;
        } else {
        		  throw new Exception(ErrorUtil.RESULT_VALUE);
        }
    }

    /**
     * 查询用户最近一个预约和未预约信息
     *
     * @return
     */
    @Override
    public LinkedList<Map<String, Object>> getMyOrderCourse() throws Exception {
        LinkedList<Map<String, Object>> myCourseOrder;
        Integer userId = ThreadLocalUtil.getUser().getUserId();
        Assert.notNull(userId, ErrorUtil.INDEX_USER_ID);
        myCourseOrder = ClientIndexDao.getMyOrderCourse(userId);
        if (myCourseOrder != null && myCourseOrder.size() > 0 && myCourseOrder.size() <= 2) {
        } else {
            throw new Exception(ErrorUtil.RESULT_VALUE);
        }
        return myCourseOrder;
    }
    /**
     * 查询文章
     * @param type
     * @param size
     * @return
     */
    @Override
    public List<Map<String, Object>> getArticle(Integer type, Integer size) throws Exception {
       ArticleType.fromId(type);
        List<Map<String, Object>> list = ClientIndexDao.getArticle(type, size);
        if(list!=null&&list.size()!=0){
            return list;
        }else{
            throw new Exception(ErrorUtil.RESULT_VALUE);
        }
    }
}
